package com.tyj.movie.util;

import com.tyj.movie.config.security.UserDetailsPacking;
import com.tyj.movie.entity.Users;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 日期: 2021/6/29 19:49
 * 作者: 谭友杰
 * 类说明:
 */
public class SecurityUtil {

    /**
     * 获取redis里的session数据 user对象
     * @return
     */
    public static Users getSessionUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //判断是否登录过
         if (authentication.getPrincipal() instanceof UserDetailsPacking){
             UserDetailsPacking userDetailsPacking = (UserDetailsPacking) authentication.getPrincipal();
             Users user1 = userDetailsPacking.getUsers();
             return user1;
         }else {
             return null;
         }

    }

    public static UserDetailsPacking getSessionUserDetailsPacking(){
        UserDetailsPacking userDetailsPacking= (UserDetailsPacking) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return userDetailsPacking;
    }
}
